package com.example.blog.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.blog.entity.Admin;
import com.example.blog.entity.AdminActionLog;
import com.example.blog.entity.User;
import com.example.blog.service.AdminService;
import com.example.blog.service.UserService;

/**
 * 管理员控制器
 */
@RestController
@RequestMapping("/api/admin")
@CrossOrigin
public class AdminController {

    @Autowired
    private AdminService adminService;
    
    @Autowired
    private UserService userService;
    
    /**
     * 管理员登录
     */
    @PostMapping("/login")
    public Map<String, Object> login(@RequestBody Map<String, String> loginInfo) {
        String loginName = loginInfo.get("loginName");
        String password = loginInfo.get("password");
        return adminService.login(loginName, password);
    }
    
    /**
     * 添加管理员
     */
    @PostMapping("/add")
    public Map<String, Object> addAdmin(@RequestBody Admin admin) {
        return adminService.addAdmin(admin);
    }
    
    /**
     * 获取所有管理员
     */
    @GetMapping("/list")
    public List<Admin> getAllAdmins() {
        return adminService.getAllAdmins();
    }
    
    /**
     * 更新管理员信息
     */
    @PutMapping("/update")
    public Map<String, Object> updateAdmin(@RequestBody Admin admin) {
        Map<String, Object> map = new HashMap<>();
        boolean result = adminService.updateAdmin(admin);
        if (result) {
            map.put("success", true);
            map.put("message", "更新成功");
        } else {
            map.put("success", false);
            map.put("message", "更新失败");
        }
        return map;
    }
    
    /**
     * 启用/禁用管理员
     */
    @PutMapping("/status/{adminId}")
    public Map<String, Object> updateAdminStatus(@PathVariable Integer adminId, @RequestParam Boolean isDisabled) {
        Map<String, Object> map = new HashMap<>();
        boolean result = adminService.updateAdminStatus(adminId, isDisabled);
        if (result) {
            map.put("success", true);
            map.put("message", isDisabled ? "禁用成功" : "启用成功");
        } else {
            map.put("success", false);
            map.put("message", isDisabled ? "禁用失败" : "启用失败");
        }
        return map;
    }
    
    /**
     * 删除管理员
     */
    @DeleteMapping("/{adminId}")
    public Map<String, Object> deleteAdmin(@PathVariable Integer adminId) {
        Map<String, Object> map = new HashMap<>();
        boolean result = adminService.deleteAdmin(adminId);
        if (result) {
            map.put("success", true);
            map.put("message", "删除成功");
        } else {
            map.put("success", false);
            map.put("message", "删除失败");
        }
        return map;
    }
    
    /**
     * 获取所有用户
     */
    @GetMapping("/users")
    public List<User> getAllUsers() {
        return userService.getAllUsers();
    }
    
    /**
     * 根据状态获取用户
     */
    @GetMapping("/users/status/{status}")
    public List<User> getUsersByStatus(@PathVariable String status) {
        return userService.getUsersByStatus(status);
    }
    
    /**
     * 更新用户状态
     */
    @PutMapping("/users/status/{userId}")
    public Map<String, Object> updateUserStatus(@PathVariable Integer userId, @RequestParam String status, 
                                           @RequestParam Integer adminId) {
        Map<String, Object> map = new HashMap<>();
        boolean result = userService.updateUserStatus(userId, status);
        
        if (result) {
            // 记录管理员操作日志
            AdminActionLog log = new AdminActionLog();
            log.setAdminId(adminId);
            log.setUserId(userId);
            log.setActionType("modify_user_info");
            log.setActionDescription("更新用户状态为" + status);
            adminService.addActionLog(log);
            
            map.put("success", true);
            map.put("message", "更新成功");
        } else {
            map.put("success", false);
            map.put("message", "更新失败");
        }
        return map;
    }
    
    /**
     * 获取操作日志
     */
    @GetMapping("/logs")
    public List<AdminActionLog> getActionLogs() {
        return adminService.getActionLogs();
    }
    
    /**
     * 根据管理员ID获取操作日志
     */
    @GetMapping("/logs/admin/{adminId}")
    public List<AdminActionLog> getActionLogsByAdminId(@PathVariable Integer adminId) {
        return adminService.getActionLogsByAdminId(adminId);
    }
    
    /**
     * 根据用户ID获取操作日志
     */
    @GetMapping("/logs/user/{userId}")
    public List<AdminActionLog> getActionLogsByUserId(@PathVariable Integer userId) {
        return adminService.getActionLogsByUserId(userId);
    }
    
    /**
     * 根据操作类型获取操作日志
     */
    @GetMapping("/logs/type/{actionType}")
    public List<AdminActionLog> getActionLogsByType(@PathVariable String actionType) {
        return adminService.getActionLogsByType(actionType);
    }
    
    /**
     * 根据时间范围获取操作日志
     */
    @GetMapping("/logs/time")
    public List<AdminActionLog> getActionLogsByTimeRange(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime) {
        return adminService.getActionLogsByTimeRange(startTime, endTime);
    }
} 